
package com.wyzz.hr.bo.impl;

import java.util.List;
import org.apache.axis.utils.StringUtils;
import org.hibernate.criterion.*;

import com.wyzz.hr.bo.IPerfcrittypeBO;
import com.wyzz.hr.dao.IPerfcrittypeDAO;
import com.wyzz.hr.pojo.Perfcrittype;


public class PerfcrittypeBOImpl implements IPerfcrittypeBO {

	public PerfcrittypeBOImpl() {
	}

	public List loadAll() {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Perfcrittype.class);
		dc.addOrder(Order.asc(Perfcrittype.PROP_PCT_SORT_ID));
		return perfcrittypeDAO.findByCriteria(dc);
	}

	public Perfcrittype save(Perfcrittype pct) {
		if (StringUtils.isEmpty(pct.getPctId())) {
			if (getPctByName(pct.getPctName()) != null) {
				pct.setInfo("EXISTED");
				return pct;
			}
			pct.setPctSortId(getMaxSortId());
			perfcrittypeDAO.saveObject(pct);
		} else {
			perfcrittypeDAO.updateObject(pct);
		}
		pct.setInfo("SUCC");
		return pct;
	}

	public void delete(String pctId) {
		perfcrittypeDAO
				.deleteObject(com.wyzz.hr.pojo.Perfcrittype.class, pctId);
	}

	public int getRelatePcCount(String pctId) {
		String hql = (new StringBuilder()).append(
				"select count(*) from Perfcrits where pcPctId.pctId='").append(
				pctId).append("'").toString();
		List list = perfcrittypeDAO.exeHqlList(hql);
		if (list != null && list.size() > 0)
			return ((Long) list.get(0)).intValue();
		else
			return 0;
	}

	public Perfcrittype getPctByName(String pctName) {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Perfcrittype.class);
		dc.add(Restrictions.eq(Perfcrittype.PROP_PCT_NAME, pctName.trim()));
		List pctList = perfcrittypeDAO.findByCriteria(dc);
		if (pctList != null && pctList.size() > 0)
			return (Perfcrittype) pctList.get(0);
		else
			return null;
	}

	public void savePctOrder(String ids[]) {
		if (ids == null || ids.length == 0)
			return;
		int sort = 1;
		int len = ids.length;
		for (int i = 0; i < len; i++) {
			perfcrittypeDAO.exeHql((new StringBuilder()).append(
					"update Perfcrittype set pctSortId=").append(sort).append(
					" where pctId ='").append(ids[i]).append("'").toString());
			sort++;
		}

	}

	public synchronized Integer getMaxSortId() {
		String hql = "select max(pctSortId) as maxSortId from Perfcrittype pct";
		List maxNoList = perfcrittypeDAO.exeHqlList(hql);
		Integer maxID;
		if (maxNoList == null || maxNoList.size() < 1
				|| maxNoList.get(0) == null)
			maxID = Integer.valueOf(1);
		else
			maxID = Integer.valueOf(1 + ((Integer) (Integer) maxNoList.get(0))
					.intValue());
		return maxID;
	}

	public IPerfcrittypeDAO getPerfcrittypeDAO() {
		return perfcrittypeDAO;
	}

	public void setPerfcrittypeDAO(IPerfcrittypeDAO perfcrittypeDAO) {
		this.perfcrittypeDAO = perfcrittypeDAO;
	}

	private IPerfcrittypeDAO perfcrittypeDAO;
}
